package cn.lcvc.xnc.application.web.action.backstage.waste;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.lcvc.xnc.application.common.model.result.ResponseData;
import cn.lcvc.xnc.application.model.WasteDisposalMapping;
import cn.lcvc.xnc.application.model.WasteDisposalMappingPicture;
import cn.lcvc.xnc.application.model.query.WasteDisposalMappingPictureQuery;
import cn.lcvc.xnc.application.model.query.WasteDisposalMappingQuery;
import cn.lcvc.xnc.application.model.query.WasteDisposalSiteVirescencePictureQuery;
import cn.lcvc.xnc.application.service.WasteDisposalMappingPictureService;
import cn.lcvc.xnc.application.service.WasteDisposalMappingService;
import cn.lcvc.xnc.application.service.WasteDisposalSiteService;
import cn.lcvc.xnc.authority.config.security.UserUtil;
import cn.lcvc.xnc.authority.model.SysUser;
import cn.lcvc.xnc.authority.web.controller.backstage.BaseBackstageController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 测绘管理模块控制器
 *
 * @author 王秋林
 * @date 2022/08/07
 */
@Api(tags = "后台的专业模块")
@RestController
@RequestMapping(value = "/api/backstage/waste/wastedisposalmapping")
public class WasteDisposalMappingController extends BaseBackstageController<WasteDisposalMapping, WasteDisposalMappingQuery,Integer> {

    @Autowired
    private WasteDisposalMappingService baseService;

    @Autowired
    private WasteDisposalSiteService wasteDisposalSiteService;

    @Autowired
    private WasteDisposalMappingPictureService wasteDisposalMappingPictureService;
    @ApiOperation(value = "查询所有测绘管理记录")
    @GetMapping("/all")
    @Override
    protected ResponseData query(WasteDisposalMappingQuery query) {
        return super.query(query);
    }

    @ApiOperation(value = "分页查询测绘管理列表")
    @Override
    public ResponseData query(Integer page, Integer pageSize, WasteDisposalMappingQuery query){
        return super.query(page,pageSize,query);
    }

    @ApiOperation(value = "读取测绘管理信息")
    @Override
    protected ResponseData get(@PathVariable("id") Integer id) {
        return super.get(id);
    }

    @ApiOperation(value = "批量删除指定的多个测绘管理信息")
    @Override
    protected ResponseData deletes(@NotEmpty(message = "操作失败：请选择要移除的记录") Integer[] ids) {
        return super.deletes(ids);
    }


    @ApiOperation(value = "添加测绘管理记录")
    @Override
    protected ResponseData add(@Valid @RequestBody WasteDisposalMapping wasteDisposalMapping) {
        return super.add(wasteDisposalMapping);
    }

    @ApiOperation(value = "编辑测绘管理")
    @Override
    protected ResponseData edit(@Valid @RequestBody WasteDisposalMapping wasteDisposalMapping) {
        return super.edit(wasteDisposalMapping);
    }


//    //======================================扩展操作=====================================================//

    @ApiOperation("测绘图片")
    @PostMapping("/uploadimg")
    public ResponseData uploadImage(@NotNull(message = "请传入测绘管理id") Integer id, @NotNull(message = "请选择上传文件") MultipartFile[] file){

        List<String> list=wasteDisposalMappingPictureService.uploadImgFile(id,file);
        return ResponseData.success(list);
    }
    @ApiOperation("测绘图片url")
    @PostMapping("/uploadimgurl")
    public ResponseData uploadImageurl(Integer mappingId){
        WasteDisposalMappingPictureQuery query = new WasteDisposalMappingPictureQuery();
        query.setMappingId(mappingId);
        return ResponseData.success(wasteDisposalMappingPictureService.findAll(query));
//        return ResponseData.success(wasteDisposalSiteVirescencePictureService.get(virescenceId));

    }
    @ApiOperation("测绘图片url")
    @PostMapping("/uploadimgurlall")
    public ResponseData uploadImageurlall(){
        return ResponseData.success(wasteDisposalMappingPictureService.findAll(null));
    }


//    @ApiOperation(value = "上传消纳场图片")
//    @PostMapping("/session/uploadPhoto")
//    public ResponseData uploadImage(@NotNull(message="请选择上传文件") MultipartFile file, @Valid @RequestBody WasteDisposalMappingPicture wasteDisposalMappingPicture){
//        // 获取当前登录用户
////        SysUser loginUser = UserUtil.getLoginUser();
////        WasteDisposalMappingPicture wasteDisposalMappingPicture = new WasteDisposalMappingPicture();
//        // 为该头像，并获取上传成功后的头像图片网址
//        String fileName=wasteDisposalMappingPictureService.uploadImgFile(wasteDisposalMappingPicture,file);
//        return ResponseData.success(fileName);
//    }


//    /**
//     * 上传表格，并导入到项目中
//     * @param file 要上传的excel
//     * @return
//     */
//    @ApiOperation(value = "导入账户表excel数据")
////    @PreAuthorize("hasAuthority('sys:user:manage:excel:upload')")
//    @PostMapping("/excel")
//    public ResponseData uploadExcel(MultipartFile file) throws Exception{
//        if(file!=null&&!file.isEmpty()){
//            String fileName=file.getOriginalFilename();//获取文件名
//            String extensionName=FileUtil.extName(fileName);//获取文件扩展名
//            //验证表格的格式是否正确
//            MyFileUpload.validateExtByDir(extensionName,"table");
//            //执行表格导入
//            ExcelImportResult result=schoolMajorExcelService.addMajorFromExcel(file.getInputStream(), UserUtil.getLoginUser());
//            //String filePath=uploadFolder+Constant.MATERIAL_EXCEL_UPLOAD_PATH;//获取excel上传后保存的物理路径
//            return ResponseData.success(result);
//        }else{
//            throw new MyWebException("操作失败：请选择上传文件");
//        }
//    }
//
//    /**
//     * 导出表格模板
//     */
//    @ApiOperation(value = "导出专业模块的excel数据")
////    @PreAuthorize("hasAuthority('sys:user:manage:excelData:download')")
//    @GetMapping("/excel")
//    public void downloadExcel(HttpServletResponse response, SchoolMajorQuery schoolMajorQuery){
//        ExcelWriter writer=schoolMajorExcelService.exportExcel(false,schoolMajorQuery);
//        MyTableUtil.downloadExcelByHutool(response,"账户表格",writer);
//    }
//
//    /**
//     * 导出表格
//     */
//    @ApiOperation(value = "导出专业的excel模板")
////    @PreAuthorize("hasAuthority('sys:user:manage:excelTemplate:download')")
//    @GetMapping("/excel/template")
//    public void downloadExcelTemplate(HttpServletResponse response){
//        ExcelWriter writer=schoolMajorExcelService.exportExcel(true,null);
//        MyTableUtil.downloadExcelByHutool(response,"账户表格模板",writer);
//    }

}
